package com.example.hyznbk.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.hyznbk.R;
import com.example.hyznbk.utils.DataCleanManager;
import com.example.hyznbk.utils.DensityUtils;
import com.example.hyznbk.utils.DialogHelper;
import com.example.hyznbk.view.activity.FavouriteListActivity;
import com.example.hyznbk.view.activity.ReviewTemplateActivity;
import com.example.hyznbk.view.activity.SystemHelperActivity;
import com.example.hyznbk.view.base.BaseFragment;
import com.example.hyznbk.view.widget.CustomDialog;
import com.lancoo.cpbase.authentication.activities.LoginActivity;

import com.example.hyznbk.view.widget.CircleImageView;
import com.lancoo.cpbase.authentication.base.LoginOperate;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;


/**
 * Created by Administrator on 2017/2/6.
 */
public class AboutMeFragment extends BaseFragment implements View.OnClickListener{

    private RelativeLayout mItemFavorites;
    private RelativeLayout mItemReviewModel;
    private RelativeLayout mItemClearCache;
    private RelativeLayout mItemSystemHelper;
    private TextView mTvLoginOut;
    private CircleImageView mImgHead;
    private TextView mTvUserName;
    private TextView mTvUserClass;
    private TextView mTvUserNo;
    //private Toolbar mToolbar;
    private LoginOperate mLoginOperate;
    private TextView mTvCache;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void updateViews(boolean isShowLoading) {
        //setHasOptionsMenu(false);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_about_me;
    }

    @Override
    public void initViews(View view, Bundle savedInstanceState) {
        mItemFavorites = (RelativeLayout) view.findViewById(R.id.item_setting_favorites);
        mItemReviewModel = (RelativeLayout) view.findViewById(R.id.item_setting_model);
        mItemClearCache = (RelativeLayout) view.findViewById(R.id.item_setting_clear_cache);
        mItemSystemHelper = (RelativeLayout)view.findViewById(R.id.item_system_helper);
        mTvCache = (TextView) mItemClearCache.findViewById(R.id.tv_cache_size);

        View toolBarLayout = view.findViewById(R.id.rel_toolbar_layout);
        ((TextView)toolBarLayout.findViewById(R.id.tv_toolbar_title)).setText(R.string.me);
        toolBarLayout.findViewById(R.id.img_main_menu).setVisibility(View.GONE);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) toolBarLayout.getLayoutParams();
        layoutParams.setMargins(0,getStatusBarHeight(),0,0);
        //toolBarLayout.setMa(0,getStatusBarHeight(),0,0);
       /* mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolBar(mToolbar,false,R.string.me);*/
        mItemFavorites.setOnClickListener(this);
        mItemReviewModel.setOnClickListener(this);
        mItemClearCache.setOnClickListener(this);
        mItemSystemHelper.setOnClickListener(this);
        mTvLoginOut = (TextView) view.findViewById(R.id.tv_login_out);
        mTvLoginOut.setOnClickListener(this);
        mImgHead = (CircleImageView) view.findViewById(R.id.img_userHead);
        mTvUserName = (TextView) view.findViewById(R.id.tv_userName);
        mTvUserClass = (TextView) view.findViewById(R.id.tv_userClass);
        mTvUserNo = (TextView) view.findViewById(R.id.tv_userNo);

        ViewCompat.setElevation(mTvLoginOut, DensityUtils.dp2px(getHolderActivity(),2));
        Glide.with(this).load(UserInfo.PhotoUrl).skipMemoryCache(false).error(R.mipmap.img_head_default).into(mImgHead);
        mTvUserName.setText(UserInfo.StuName);
        mTvUserClass.setText(UserInfo.ClassName);
        mTvUserNo.setText(UserInfo.StuId);
        mLoginOperate = new LoginOperate(getHolderActivity());


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.item_setting_favorites:
                startActivity(new Intent(getHolderActivity(),FavouriteListActivity.class));
                break;
            case R.id.item_setting_model:
                ReviewTemplateActivity.launch(getHolderActivity());
                break;
            case R.id.item_setting_clear_cache:

                DialogHelper.cleanCacheDialog(getHolderActivity(), mTvCache.getText().toString(),new CustomDialog.OnCustomClickListener() {
                    @Override
                    public void onClick() {
                        DataCleanManager.cleanInternalCache(getHolderActivity());
                        mTvCache.setText("0KB");
                    }
                });
                break;
            case R.id.item_system_helper:
                SystemHelperActivity.launch(getHolderActivity());
                break;
            case R.id.tv_login_out:
                mLoginOperate.logOut();
                startActivity(new Intent(getHolderActivity(),LoginActivity.class));
                getHolderActivity().finish();
                break;
        }

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser){
            if (mTvCache != null){
                try {
                    mTvCache.setText("计算中...");
                    String cacheSize = DataCleanManager.getCacheSize(getHolderActivity());
                    mTvCache.setText(cacheSize);
                } catch (Exception e) {
                    e.printStackTrace();
                    mTvCache.setText("计算失败");
                }

            }
        }
    }
}